<?php
namespace Rest\Controller;
use Think\Controller\RestController;

class CommonController extends RestController {

    public function __construct() {
        parent::__construct();
        set_exception_handler(array($this,'exception_error'));


        $code = I('get.code');
        verify($code,'notEmpty','标识符为空');

        $token = I('get.token');
        verify($token,'notEmpty','令牌环丢失');

        $timestamp = I('get.timestamp');
        verify($timestamp,'notEmpty','非法过期时间');

        $secret = C('TOKEN.' . $code);
        verify($secret,'notEmpty','权限失效');

        if(substr(md5($timestamp . $secret),0,8) !== $token) {
            E('无权访问');
        }
    }

    public function error($message,$code = 0) {
        echo json_encode(array(
            'code'      =>  $code,
            'message'   =>  $message
        ));exit();
    }

    public function success($data) {
        echo json_encode(array(
            'code'      =>  200,
            'data'      =>  $data,
        ));exit();
    }

    public function exception_error(\Exception $e) {
        $this->error($e->getMessage(),$e->getCode());
    }
}
?>
